<template>
  <div class="rule-container">
    <div class="create-container" style="display: flex">
      <el-button type="primary" @click="dialogVisible = true"
        >增加停车计费规则</el-button
      >
      <el-button @click="exportToExcel" type="text" style="padding-left: 30px"
        >导出Excel</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="{ row }">
            {{ formateChargeType(row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <!-- 总页数 = 总条数/页容量 -->
      <el-pagination
        style="float: right; margin-top: 10px"
        layout="total,prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        @current-change="handleChange"
      >
      </el-pagination>
    </div>
    <!-- 弹框 -->
    <AddRule
      :dialogVisible="dialogVisible"
      @getList="getRuleList()"
      @cls="dialogVisible = false"
    />
  </div>
</template>

<script>
import { reqGetRuleListAPI } from "@/api/carrule.js";
// 创建工作簿 创建工作表 一共5步
import { utils, writeFileXLSX } from "xlsx";
import AddRule from "./components/addRule.vue";
export default {
  name: "Building",
  components: {
    AddRule,
  },
  data() {
    return {
      ruleList: [],
      params: {
        page: 1, //当前页码
        pageSize: 10, //页容量
      },
      total: null,
      dialogVisible: false, // 控制弹框关闭打开
    };
  },
  created() {
    this.getRuleList();
  },
  methods: {
    //获取列表
    async getRuleList() {
      const { data } = await reqGetRuleListAPI(this.params);
      this.ruleList = data.rows;
      this.total = data.total;
    },
    handleChange(e) {
      this.params.page = e;
      this.getRuleList();
    },
    indexMethod(row) {
      // 当前页码减1 * 当前页容量 + 当前行号（从0开始） + 1
      return (this.params.page - 1) * this.params.pageSize + row + 1;
    },
    // excel导出
    async exportToExcel() {
      const {
        data: { rows },
      } = await reqGetRuleListAPI(this.params);
      console.log(rows);
      // 设置导出的规则，表头：导出的字段名
      const tableHeaderKeys = [
        "ruleNumber",
        "ruleName",
        "freeDuration",
        "chargeCeiling",
        "chargeType",
        "ruleNameView",
      ];
      // 去每一个item里取出需要的数据返回一个新数组
      const newList = rows.map((item) => {
        // 创建一个新对象来存储
        const obj = {};
        // 遍历tableHeaderKeys，查找符合的属性
        tableHeaderKeys.forEach((key) => {
          // 如果当前obj有这个属性，那就退出
          if (obj[key]) {
            this.$message.error("数据格式错误,已经有了");
            return;
          }
          obj[key] = item[key];
        });
        return obj;
      });
      // 表头中文字段value
      const tableHeaderValues = [
        "计费规则编号",
        "计费规则名称",
        "免费时长(分钟)",
        "收费上线(元)",
        "计费方式",
        "计费规则",
      ];
      // 创建工作簿
      const workbook = utils.book_new();
      // 创建工作表，要求是一个对象数组格式
      const worksheet = utils.json_to_sheet(newList);
      // 把工作表塞入工作簿中
      utils.book_append_sheet(workbook, worksheet, "计费规则");
      // 改写表头 sheet_add_aoa(表名，[[二维数组表头]]，{origin: 'A1'}从哪里开始排)
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], {
        origin: "A1",
      });
      // 导出方法进行导出
      writeFileXLSX(workbook, "计费规则.xlsx");
    },
    // 格式化类型
    formateChargeType(val) {
      const map = {
        duration: "时长收费",
        turn: "按次收费",
        partition: "分段收费",
      };
      return map[val];
    },
  },
};
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
</style>
